package com.nuc.bigdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nuc.bigdata.orm.entity.*;
import com.nuc.bigdata.orm.feign.CommodityFeign;
import com.nuc.bigdata.orm.mapper.*;
import com.nuc.bigdata.service.HomeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author JackLuo
 * @date 2019/11/28
 */
@Service
public class HomeInfoServiceImpl implements HomeInfoService {

    private final CommodityMapper goodsMapper;
    private final CommodityPictureMapper pictureMapper;
    private final CommoditySellMapper sellMapper;
    private final CommodityClicksMapper clicksMapper;
    private final CommodityViewPagerMapper viewPagerMapper;
    private final static Long LENGTH =6L;

    @Autowired
    HomeInfoServiceImpl(CommodityMapper goodsMapper,
                        CommodityPictureMapper pictureMapper,
                        CommoditySellMapper sellMapper,
                        CommodityClicksMapper clicksMapper,
                        CommodityViewPagerMapper viewPagerMapper) {
        this.goodsMapper = goodsMapper;
        this.pictureMapper = pictureMapper;
        this.sellMapper = sellMapper;
        this.clicksMapper = clicksMapper;
        this.viewPagerMapper = viewPagerMapper;
    }

    /**
     * 列出所有商品
     * @param pageSize 查询页码
     * @return 返回给front服务的商品ArrayList
     */
    @Override
    public ArrayList<CommodityFeign> listAll(Long pageSize) {
        Long offset = (pageSize-1) * LENGTH;
        List<Commodity> goodsList = goodsMapper.listAllToPage(offset,LENGTH );
        ArrayList<CommodityFeign> feignArrayList = new ArrayList<>();
        for (Commodity goods:goodsList) {
            CommodityFeign commodityFeign = transformCommodity(goods);
            feignArrayList.add(commodityFeign);
        }
        return feignArrayList;
    }

    /**
     * 根据商品唯一标识获取商品信息
     *
     * @param commodityId 商品唯一标识
     * @return 商品信息
     */
    @Override
    public CommodityFeign queryByCommodityId(String commodityId) {
        Commodity commodity = goodsMapper.selectByCommodityId(commodityId);
        return transformCommodity(commodity);
    }


    /**
     * 将商品实体转化为提供给front服务的feign对象
     * @param goods 商品实体类
     * @return 商品feign类对象
     */
    private CommodityFeign transformCommodity(Commodity goods) {
        CommodityFeign commodityFeign = new CommodityFeign();
        // 设置商品基本信息
        String commodityId = goods.getCommodityId();
        commodityFeign.setTitle(goods.getTitle());
        commodityFeign.setAbstractMsg(goods.getAbstractMsg());
        commodityFeign.setCommodityId(commodityId);
        commodityFeign.setHomePic(goods.getPictureId());
        commodityFeign.setStickerPrice(goods.getStickerPrice());
        // 设置点击数
        CommodityClicks clicks = queryClickByCommodityId(commodityId);
        if (ObjectUtils.isEmpty(clicks)) {
            commodityFeign.setClick(0L);
        }else {
            commodityFeign.setClick(clicks.getClick());
        }
        // 设置商品上架等信息
        CommoditySellDetails sellDetails = querySellDetailsById(commodityId);
        if (ObjectUtils.isEmpty(sellDetails)) {
            Date date = new Date();
            commodityFeign.setAddTime(date);
            commodityFeign.setStock(BigDecimal.valueOf(9999));
            commodityFeign.setSellPrice(BigDecimal.valueOf(999999.00));
            commodityFeign.setCommodityUom("件");
        }else {
            commodityFeign.setAddTime(sellDetails.getCreateTime());
            commodityFeign.setStock(sellDetails.getStock());
            commodityFeign.setSellPrice(sellDetails.getSellPrice());
            commodityFeign.setCommodityUom(sellDetails.getCommodityUom());
        }


        return commodityFeign;
    }

    /**
     * 根据商品唯一标识查出它所有的图片介绍
     *
     * @param commodityId 商品唯一标识
     * @return 包含商品图片信息的ArrayList
     */
    private ArrayList<CommodityPictureDetails> listByCommodityId(String commodityId) {
        QueryWrapper<CommodityPictureDetails> qw = new QueryWrapper<>();
        qw.eq("commodity_id", commodityId);
        qw.orderByAsc("create_time");
        List<CommodityPictureDetails> pictureDetailsList = pictureMapper.selectList(qw);
        return new ArrayList<>(pictureDetailsList);
    }

    /**
     * 根据商品唯一标识获取商品点击数
     *
     * @param commodityId 商品唯一标识
     * @return 商品点击数实体类
     */
    private CommodityClicks queryClickByCommodityId(String commodityId) {
        QueryWrapper<CommodityClicks> qw = new QueryWrapper<>();
        qw.eq("commodity_id", commodityId);
        return clicksMapper.selectOne(qw);
    }

    /**
     * 根据商品唯一标识获取商品上架售卖详情
     * @param commodityId 商品唯一标识
     * @return 商品上架售卖实体
     */
    private CommoditySellDetails querySellDetailsById(String commodityId) {
        QueryWrapper<CommoditySellDetails> qw = new QueryWrapper<>();
        qw.eq("commodity_id", commodityId);
        return sellMapper.selectOne(qw);
    }
}
